package com.xiaoq;

import com.xiaoq.distribution.consumer.AbstractReceiver;
import com.xiaoq.distribution.consumer.DistributionReceiver;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
@EnableConfigurationProperties({RabbitMQQueueNameProperties.class, RabbitMQConnectionProperties.class})
public class RabbitMQConfig {
	@Autowired
	private RabbitMQConnectionProperties connectionConfig;
	
	@Autowired
	private RabbitMQQueueNameProperties queueNameConf;

	@Autowired
	private DistributionReceiver distributionReceiver;
	
	/**
	 * 用来连接MQ的
	 * @return
	 */
	@Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(connectionConfig.getHost(), connectionConfig.getPort());
        connectionFactory.setUsername(connectionConfig.getUserName());
        connectionFactory.setPassword(connectionConfig.getPassword());
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

	/**
	 * template 用来发消息的
	 * @return
	 */
	@Bean("templateA")
	public RabbitTemplate jmsTemplateReceive() {
		RabbitTemplate tpl = new RabbitTemplate();
		tpl.setConnectionFactory(connectionFactory());
		return tpl;
	}


	@Bean
	public Queue handlerDistributionQueue() {
		return new Queue(queueNameConf.getDistributionQueueName(), true);
	}


	
	@Bean
    public SimpleRabbitListenerContainerFactory myFactory(
    		SimpleRabbitListenerContainerFactoryConfigurer configurer) {
		SimpleRabbitListenerContainerFactory factory =
                new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory());
        return factory;
	}

	/**
	 * 把Queue和exchange进行绑定
	 * @return
	 */
	@Bean
	public Binding distributionBinding() {
		return BindingBuilder.bind(handlerDistributionQueue()).to(directExchange()).with(handlerDistributionQueue().getName());
	}


	@Bean
	public DirectExchange directExchange() {
		return new DirectExchange("distribution-exchange");
	}


	/**
	 * 把监听到的信息交给谁处理
	 * @return
	 */
	@Bean
	public SimpleMessageListenerContainer distributionPubContainer() {
		return build(handlerDistributionQueue(), distributionReceiver);
	}

	
	private SimpleMessageListenerContainer build(Queue queue, AbstractReceiver recv) {
		SimpleMessageListenerContainer lc = new SimpleMessageListenerContainer();
		lc.setConnectionFactory(connectionFactory());
		lc.setQueues(queue);
		MessageListenerAdapter la = new MessageListenerAdapter(recv);
		lc.setMessageListener(la);
		lc.setConcurrentConsumers(1);
//		lc.setMaxConcurrentConsumers();
		return lc;
	}

	
}
